<template>
	<div style="border: 1px solid aqua;height:500px;width: 100%;">
		<h2>登录</h2>
			<table border="1" class="ce">
				<tr><td>账号:</td><td><input type="text" name="phone"  v-model="user.username" /></td></tr>
				<tr><td>密码:</td><td><input type="password" name="pwd" v-model="user.password" /></td></tr>
				<tr><td></td><td><input type="submit" @click="login()" value="登录"/></td></tr>
			</table>
	</div>
</template>
<script setup>
import axios from 'axios';
</script>
<script>
export default{
		data(){
			return{
				user:{username:'',password:'',token:''},
			}
		},
		methods:{
			login(){
				const that=this;
				
			let token ="";
				this.$postx("/api/login",that.user)
				.then(resp =>{
					window.localStorage.setItem('token',resp.data.data.token);
					console.log(resp.data.data.token);
					token =resp.data.data.token;
					
					
					this.$get("/api/user/info")
					.then(function (resp){
						
						that.$store.state.user.name=resp.data.data.uname;
						that.$store.state.user.pwd=resp.data.data.pwd;
						that.$store.state.user.pic=resp.data.data.pic;
						that.$store.state.user.token=window.localStorage.getItem("token");
						that.$store.state.user.role=resp.data.data.role;
						console.log(that.$store.state.user);
					
					if(resp.data.data.role=='adm'){
						 that.$store.state.user.role='管理员';
						that.$router.push('/adm')
					}
					
					if(resp.data.data.role=='stu'){
						 that.$store.state.user.role='学生';
						that.$router.push('/stu')
					}
					if(resp.data.data.role=='tea'){
						that.$store.state.user.role='老师';
						that.$router.push('/tea')
					}
					})

				})
				
				
				
				
			}
		},
		mounted(){
			
		}
	}
	
</script>
<style>
	.ce{
		margin-top: auto;
		margin-right: auto;
	}
</style>
	